package com.bank.controller;

import com.bank.entity.*;
import com.bank.mapper.*;
import com.opencsv.CSVWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

@RestController
@RequestMapping("/api/export")
public class ExportController {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private CardInfoMapper cardInfoMapper;
    @Autowired
    private TradeInfoMapper tradeInfoMapper;
    @Autowired
    private DepositMapper depositMapper;

    @GetMapping("/user")
    public void exportUserData(HttpServletResponse response) throws IOException {
        response.setContentType("text/csv");
        response.setHeader("Content-Disposition", "attachment; filename=users.csv");

        List<UserInfo> users = userInfoMapper.findAll();
        try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(response.getOutputStream()))) {
            // 写入表头
            writer.writeNext(new String[]{"客户编号", "客户姓名", "身份证号", "联系电话", "联系地址"});

            // 写入数据
            for (UserInfo user : users) {
                writer.writeNext(new String[]{
                        user.getCustomerID().toString(),
                        user.getCustomerName(),
                        user.getPid(),
                        user.getTelephone(),
                        user.getAddress()
                });
            }
        }
    }

    @GetMapping("/card")
    public void exportCardData(HttpServletResponse response) throws IOException {
        response.setContentType("text/csv");
        response.setHeader("Content-Disposition", "attachment; filename=cards.csv");

        List<CardInfo> cards = cardInfoMapper.findAll();
        try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(response.getOutputStream()))) {
            // 写入表头
            writer.writeNext(new String[]{"卡号", "币种", "存款类型", "开户日期", "开户金额", "余额", "挂失状态", "客户编号"});

            // 写入数据
            for (CardInfo card : cards) {
                writer.writeNext(new String[]{
                        card.getCardID(),
                        card.getCurID(),
                        card.getSavingID().toString(),
                        card.getOpenDate().toString(),
                        card.getOpenMoney().toString(),
                        card.getBalance().toString(),
                        card.getIsReportLoss(),
                        card.getCustomerID().toString()
                });
            }
        }
    }

    @GetMapping("/trade")
    public void exportTradeData(HttpServletResponse response) throws IOException {
        response.setContentType("text/csv");
        response.setHeader("Content-Disposition", "attachment; filename=trades.csv");

        List<TradeInfo> trades = tradeInfoMapper.findAll();
        try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(response.getOutputStream()))) {
            // 写入表头
            writer.writeNext(new String[]{"交易时间", "交易类型", "卡号", "交易金额", "备注"});

            // 写入数据
            for (TradeInfo trade : trades) {
                writer.writeNext(new String[]{
                        trade.getTradeDate().toString(),
                        trade.getTradeType(),
                        trade.getCardID(),
                        trade.getTradeMoney().toString(),
                        trade.getRemark()
                });
            }
        }
    }

    @GetMapping("/deposit")
    public void exportDepositData(HttpServletResponse response) throws IOException {
        response.setContentType("text/csv");
        response.setHeader("Content-Disposition", "attachment; filename=deposits.csv");

        List<Deposit> deposits = depositMapper.findAll();
        try (CSVWriter writer = new CSVWriter(new OutputStreamWriter(response.getOutputStream()))) {
            // 写入表头
            writer.writeNext(new String[]{"类型编号", "类型名称", "描述"});

            // 写入数据
            for (Deposit deposit : deposits) {
                writer.writeNext(new String[]{
                        deposit.getSavingID().toString(),
                        deposit.getSavingName(),
                        deposit.getDescrip()
                });
            }
        }
    }
}